package com.example.demo.util;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FindAllFile {
    public static void main(String[] args){
        //要查找的项目路径
        String path = "D:\\springProject\\demo";
        File file = new File(path);

        //要查找的字符串
        List<String> tableLists = new ArrayList<>();
        tableLists.add("tablename");

        //结果,key=要查找的字符串，value=出现的次数
        Map<String,Integer> resultMap = new HashMap<>();

        readAllFile(file,tableLists,resultMap);

    }

    /**
     * 读取入口
     * @param file
     * @param tableLists
     * @param resultMap
     */
    public static void readAllFile(File file,List<String> tableLists,Map<String,Integer> resultMap){
        try{
            if(file.isFile()){
                checkTableName(file,tableLists,resultMap);
            }
            else if(file.isDirectory()){
                File[] childFiles = file.listFiles();
                for(File childFile : childFiles){
                    if(childFile.isFile()){
                        checkTableName(childFile,tableLists,resultMap);
                    }
                    else if(childFile.isDirectory()){
                        readAllFile(childFile,tableLists,resultMap);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 校验文件
     * @param file
     * @param tableLists
     * @param resultMap
     * @throws Exception
     */
    public static void checkTableName(File file,List<String> tableLists,Map<String,Integer> resultMap) throws Exception{
        String path = file.getAbsolutePath();
        System.out.println("开始查找文件："+path);

        String content = readFileContent(new FileInputStream(file),"utf-8").toLowerCase();
        for(int i=0;i<tableLists.size();i++){
            String tableName = tableLists.get(i);
            if(content.indexOf(tableName) >= 0){
                if(resultMap.get(tableName) == null){
                    resultMap.put(tableName,1);
                }
                else {
                    int index = resultMap.get(tableName);
                    index ++;
                    resultMap.put(tableName,index);
                }
            }
        }
    }

    /**
     * 读取文件内容
     * @param inputStream
     * @param charset
     * @return
     */
    public static String readFileContent(InputStream inputStream,String charset) {
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            inputStreamReader = new InputStreamReader(inputStream,charset);
            reader = new BufferedReader(inputStreamReader);
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr + "\n");
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    inputStreamReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}
